package guoanmaker.personal.business.service;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import guoanmaker.personal.business.model.PersonalAddress;
import guoanmaker.personal.business.model.PersonalUser;
import guoanmaker.personal.business.model.ReturnResult;
import guoanmaker.personal.business.repository.PersonalAddressRepository;
import guoanmaker.personal.business.repository.PersonalUserRepository;

@Service
public class PersonalAddressService {
	
	@Resource
	private PersonalAddressRepository personalAddressRepository;
	
	@Resource
	private PersonalUserRepository personalUserRepository;
	/**
	 * 
	* @Description: 保存地址 
	* @param name 姓名
	* @param address 地址
	* @param telephone 电话
	* @param isdefault 是否为默认0为否 1为是
	* @param user_id 用户id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月16日 上午11:15:43     
	* @return ReturnResult    
	* @throws 
	 */
	@Transactional
	public ReturnResult saveAddress(String name,String address,String telephone,String isdefault,String user_id) {
		ReturnResult returnResult=new ReturnResult();
		PersonalUser user=personalUserRepository.findOne(user_id);
		if(user==null){
			returnResult.setKey("error");
			returnResult.setValue("用户不存在");
		}else{
			PersonalAddress personalAddress=new PersonalAddress();
			personalAddress.setUser(user);
			personalAddress.setName(name);
			personalAddress.setAddress(address);
			personalAddress.setTelephone(telephone);
			if(isdefault!=null&&isdefault.equals("1")){
				personalAddressRepository.updateAddressIsdefault(user_id);
				personalAddress.setIsdefault(isdefault);
			}else{
				personalAddress.setIsdefault("0");
			}
			personalAddressRepository.save(personalAddress);
			returnResult.setKey("success");
    		returnResult.setValue("保存地址成功");
		}
		return returnResult;
	}
	
	/**
	 * 
	* @Description: 删除地址
	* @param @param id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月16日 下午1:53:35     
	* @return ReturnResult    
	* @throws 
	 */
	@Transactional
	public ReturnResult deleteAddress(String id){
		ReturnResult returnResult=new ReturnResult();
		personalAddressRepository.delete(id);
		returnResult.setKey("success");
		returnResult.setValue("删除地址成功");
		return returnResult;
	}
	
	/**
	 * 
	* @Description: 保存地址 
	* @param name 姓名
	* @param address 地址
	* @param telephone 电话
	* @param isdefault 是否为默认0为否 1为是
	* @param id 地址id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月16日 下午2:10:12     
	* @return ReturnResult    
	* @throws 
	 */
	@Transactional
	public ReturnResult updateAddress(String name,String address,String telephone,String isdefault,String id) {
		ReturnResult returnResult=new ReturnResult();
		PersonalAddress personalAddress=personalAddressRepository.findOne(id);
		if(personalAddress==null){
			returnResult.setKey("error");
			returnResult.setValue("地址不存在");
		}else{
			if(name!=null&&!name.equals("")){
				personalAddress.setName(name);
			}
			
			if(address!=null&&!address.equals("")){
				personalAddress.setAddress(address);
			}
			
			if(telephone!=null&&!telephone.equals("")){
				personalAddress.setTelephone(telephone);
			}
			if(isdefault!=null&&!isdefault.equals("")){
				if(isdefault.equals("1")&&(personalAddress.getIsdefault()==null||!personalAddress.getIsdefault().equals("1"))){
					personalAddressRepository.updateAddressIsdefault(personalAddress.getUser().getId());
				}
				personalAddress.setIsdefault(isdefault);
			}
			
			personalAddressRepository.save(personalAddress);
			returnResult.setKey("success");
    		returnResult.setValue("保存地址成功");
		}
		return returnResult;
	}

}
